//
//  ClockAlertView.swift
//  SkyWeather
//
//  Created by Baiy on 2020/11/5.
//

import UIKit
import SnapKit

class SkyAlertView: UIView {

    let INTERVAL: CGFloat = 24
    let HEADER_HEIGHT = HEIGHT * 0.50

    enum AlertType {
        case single
        case double
    }

    class func show(type: AlertType, title: String, desc:String, handle:@escaping () -> Void) {
        let view = SkyAlertView(frame: CGRect(x: 0, y: 0, width: WIDTH, height: HEIGHT))
        AppTool.rootView()?.addSubview(view)
        
        view.show(type: type, title: title, desc: desc, handle: handle)
    }
    
    func show(type: AlertType, title: String, desc:String, handle:@escaping () -> Void) {
        self.show()
        
        let bottom_space: CGFloat = 120
        let font = UIFont.systemFont(ofSize: 14, weight: .light)
        let desc_height = min(HEADER_HEIGHT - bottom_space - 48 - STATUEBAR_HEIGHT, (desc as NSString).height(for: font, width: width - INTERVAL * 4))
        
        let descLabel = UILabel(string: desc, fontSize: 14, frame: CGRect(x: INTERVAL * 2, y: HEADER_HEIGHT - desc_height - bottom_space, width: width - INTERVAL * 4, height: desc_height), color: .detail, textAlignment: .center)
        descLabel.font = font
        descLabel.adjustsFontSizeToFitWidth = true
        self.headerView.addSubview(descLabel)
        
        let line = UIView(frame: CGRect(x: INTERVAL * 2, y: descLabel.top - INTERVAL, width: width - INTERVAL * 4, height: 1))
        line.backgroundColor = .line
        self.headerView.addSubview(line)
        
        let titleLabel = UILabel(string: title, fontSize: 18, frame: CGRect(x: INTERVAL, y: line.top - INTERVAL - 24, width: width - INTERVAL * 2, height: 24), color: .title, textAlignment: .center)
        self.headerView.addSubview(titleLabel)
        
        let actions = type == .single ? ["确定"] : ["取消","确定"]
        let btn_width = width * 0.25
        let btn_height: CGFloat = 36
        let btn_interval = width * 0.10
        let btn_space = (width - btn_width * CGFloat(actions.count) - btn_interval * CGFloat(actions.count - 1)) * 0.5
        let btn_top = descLabel.bottom + INTERVAL * 2
        for index in 0..<actions.count {
            let button = UIButton(title: actions[index], titleColor: .bg, fontSize: 12, frame: CGRect(x: btn_space + (btn_width + btn_interval) * CGFloat(index), y: btn_top, width: btn_width, height: btn_height)) { (btn) in
                
                if index == actions.count - 1 {
                    handle()
                }
                
                self.disappear()
                
            }
            button.setCornerRadius(radius: btn_height * 0.5)
            self.headerView.addSubview(button)
            
            if index == actions.count - 1 {
                button.setThemeGradient()
            }else {
                button.backgroundColor = .detail
            }
        }
        
    }
    
    private func show() {
        self.maskBGView.alpha = 0
        self.headerView.snp.updateConstraints { (make) in
            make.top.equalTo(-HEADER_HEIGHT)
        }
        self.layoutIfNeeded()
        
        self.headerView.snp.updateConstraints { (make) in
            make.top.equalTo(0)
        }
        
        UIView.animate(withDuration: 0.32, animations: {
            self.maskBGView.alpha = 0.72
            self.layoutIfNeeded()
        }) { (complete) in
            
        }
    }
    
    @objc private func disappear() {
        self.maskBGView.alpha = 0.72
        self.headerView.snp.updateConstraints { (make) in
            make.top.equalTo(-HEADER_HEIGHT)
        }
        
        UIView.animate(withDuration: 0.32, animations: {
            self.alpha = 0
            self.layoutIfNeeded()
        }) { (complete) in
            self.removeFromSuperview()
        }
    }
    
    lazy var headerView:UIView = {
        let view = UIView()
        view.backgroundColor = .bg
        self.insertSubview(view, aboveSubview: self.maskBGView)
        
        view.snp.makeConstraints({ (make) in
            make.left.right.equalTo(self)
            make.top.equalTo(-HEADER_HEIGHT)
            make.height.equalTo(HEADER_HEIGHT)
        })
        
        return view
    }()
    
    lazy var maskBGView:UIView = {
        let maskBGView = UIView()
        maskBGView.backgroundColor = .shadow
        self.addSubview(maskBGView)
        
        //实现模糊效果
        let effect = UIBlurEffect.init(style: UIColor.darkMode ? .dark : .light)
        let effectView = UIVisualEffectView.init(effect: effect)
        maskBGView.addSubview(effectView)
        
        maskBGView.snp.makeConstraints({ (make) in
            make.edges.equalTo(self)
        })
        
        effectView.snp.makeConstraints({ (make) in
            make.edges.equalTo(maskBGView)
        })
        
        return maskBGView
    }()

}
